const { makeInvoker } = require('awilix-express')
const { Router } = require('express')
const { responseHelper } = require('../helper')

const api = makeInvoker(
  ({ getMenuTree, getMenuDetail, createMenu, updateMenu, deleteMenu }) => {
    return {
      getMenuTree: responseHelper(async (req, res) => {
        return await getMenuTree.execute(req.query)
      }),
      getMenuDetail: responseHelper(async (req, res) => {
        return await getMenuDetail.execute(req.params.id)
      }),
      createMenu: responseHelper(async (req, res) => {
        return await createMenu.execute(req.body)
      }),
      updateMenu: responseHelper(async (req, res) => {
        return await updateMenu.execute({
          id: req.params.id,
          ...req.body,
        })
      }),
      deleteMenu: responseHelper(async (req, res) => {
        return await deleteMenu.execute(req.params.id)
      }),
    }
  }
)

module.exports = ({ all }) => {
  const router = Router()
  router
    .get('/tree', all, api('getMenuTree'))
    .post('/', all, api('createMenu'))
    .get('/:id', all, api('getMenuDetail'))
    .put('/:id', all, api('updateMenu'))
    .delete('/:id', all, api('deleteMenu'))
  return router
}
